package com.etc.RCCPA001;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.etc.common.CodeMessage;
import com.etc.common.SessionCode;

/**
 * 
 * Filename:    GetInfoContoller.java  
 * Description: 登录后获取用户信息   
 * Copyright:   Copyright (c) 2018-2019 All Rights Reserved.
 * Company:     chinasofti.com Inc.
 * @author:     mazheng 
 * @version:    1.0  
 * Create at:   2020年3月15日 下午2:58:16  
 *  
 * Modification History:  
 * Date         Author      Version     Description  
 * ------------------------------------------------------------------  
 * 2020年3月15日      mazheng      1.0         1.0 Version  
 *
 */
@RestController
@RequestMapping("admin")
public class GetInfoContoller {

    @RequestMapping("info.action")
    public CodeMessage getInfo(@RequestParam("token") String map1, HttpServletRequest req) {
        
//        String userJson = "{\"token\":\"" + map1 + "\"}";
        HttpHeaders headers = new HttpHeaders();
        List<String> cookies =new ArrayList<String>();
        /* 登录获取Cookie 这里是直接给Cookie，可使用下方的login方法拿到Cookie给入*/
        MediaType type = MediaType.parseMediaType("application/x-www-form-urlencoded; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        // form fangshi
        MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();      //请求体给予内容
        map.set("token", map1);
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);
//        HttpEntity<String> formEntity = new HttpEntity<String>(userJson, headers);
        ResponseEntity<CodeMessage> response = restTemplate.postForEntity("http://SERVICE-USER-CENTER/admin/info", request, CodeMessage.class);
        CodeMessage cm = response.getBody();
        
        HttpSession session = req.getSession(true);
        session.setAttribute(SessionCode.ADMINUSER, ((Map)cm.getData()).get("username"));
        
        return cm;
    }
    
    @Autowired
    RestTemplate restTemplate;

}
